﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helper.Business.Repository;
using Helper.Business.Implementation;
using Helper.Core.Entity;

namespace Helper.Controllers
{
    public partial class SectorController : BaseController
    {

        internal ISectorRepository _SectorRepository;


        public SectorController()
        {
            if (_SectorRepository == null)
                _SectorRepository = new SectorRepository();

            this.LoginNeeded = true;
        }

        //
        // GET: /Sector/

        public ActionResult Index()
        {
            return View(_SectorRepository.GetAll());
        }

        //
        // GET: /Sector/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Sector/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Sector/Create

        [HttpPost]
        public ActionResult Create(Sector collection)
        {
            try
            {
                // TODO: Add insert logic here
                _SectorRepository.Add(collection);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Sector/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_SectorRepository.FindById(id));
        }

        //
        // POST: /Sector/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Sector collection)
        {
            try
            {
                // TODO: Add update logic here
                Sector oldSec = _SectorRepository.FindById(id);

                oldSec.Name = collection.Name;

                _SectorRepository.Update(oldSec);
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Sector/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_SectorRepository.FindById(id));
        }

        //
        // POST: /Sector/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                _SectorRepository.Delete(_SectorRepository.FindById(id));
                return View();
            }
            catch
            {
                return View();
            }
        }


    }
}
